﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string sqlStr = "Data source=.;Initial Catalog=test;User ID=hzh;Password=hzh2000623";
            try
            {
                using (SqlConnection connection = new SqlConnection(sqlStr))
                {
                    connection.Open();
                    MessageBox.Show("数据库连接成功！");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("数据库连接失败！" + ex.Message);
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sqlStr = "Data source=.;Initial Catalog=test;User ID=hzh;Password=hzh2000623";
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(sqlStr);

                conn.Open();

                string sql = "Select count(*) from userinfo where name='{0}' and password ='{1}'";
                sql = string.Format(sql, textBox1.Text, textBox2.Text);

                SqlCommand cmd = new SqlCommand(sql, conn);

                int returnvalue = (int)cmd.ExecuteScalar();

                if (returnvalue != 0)
                {
                    MessageBox.Show("登录成功！");
                }
                else
                {
                    MessageBox.Show("登录失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("注册失败" + ex.Message);

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
